package com.eboss2.sbs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.eboss2.sbs.R;
import com.eboss2.sbs.activity.base.BaseActivity;
import com.eboss2.sbs.config.Config;
import com.eboss2.sbs.http.HttpUtil;
import com.eboss2.sbs.myapplication.MyApplication;
import com.loopj.android.http.RequestParams;
import com.mytools.tool.activityManager.MyActivityManager;
import com.tool.utils.utils.StringUtils;
import com.tool.utils.utils.ToastUtils;
import com.tool.utils.utils.UtilPreference;
import com.tool.utils.view.MoneyEditText;
import com.tool.utils.view.MyCountDownTimer;
import com.tool.utils.view.ShapeView;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyTxActivity extends BaseActivity {

    @BindView(R.id.titleback_btn_back)
    LinearLayout titlebackBtnBack;
    @BindView(R.id.tv_bankName)
    TextView tvBankName;
    @BindView(R.id.tv_accountName)
    TextView tvAccountName;
    @BindView(R.id.tv_bank_card)
    TextView tvBankCard;
    @BindView(R.id.tv_balance)
    TextView tvBalance;
    @BindView(R.id.et_tx_amt)
    MoneyEditText etTxAmt;
    @BindView(R.id.et_user_phone)
    EditText etUserPhone;
    @BindView(R.id.tv_getcode)
    TextView tvGetcode;
    @BindView(R.id.et_tx_code)
    EditText etTxCode;
    @BindView(R.id.btn_commit)
    ShapeView btnCommit;
    @BindView(R.id.all_tx)
    TextView allTx;


    private int balanceAmt;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_mytx);
        ButterKnife.bind(this);
        MyActivityManager.getInstance().addActivity(this);
        initView();
        getdata();
    }

    private void initView() {
        RelativeLayout relativeLayout = relativeLayout(R.id.lv_heard);
        relativeLayout.setBackgroundColor(getResources().getColor(R.color.statusBar));
        showView(R.id.titleback_btn_back, true);
        setTvText(R.id.tv_title, "提现");
        showView(R.id.next_sure, false);


        balanceAmt = getIntent().getIntExtra("balance", 0);

        tvBalance.setText(StringUtils.formatIntMoney(balanceAmt));
    }


    private void getdata() {
        showProgress("加载中...");

        String url = Config.SBS_TX_INFO;
        RequestParams params = new RequestParams();
        params.put("shopId", MyApplication.getmInstance().getLoginMEntity().getShopid() + "");

        HttpUtil.get(url, params, new HttpUtil.RequestListener() {

            @SuppressWarnings("unchecked")
            @Override
            public void success(String response) {
                disShowProgress();
                try {


                    JSONObject result = new JSONObject(response);
                    if (!result.optBoolean("status")) {
                        showErrorMsg(result.getString("message"));
                        return;
                    }

                    JSONObject obj = result.getJSONObject("data");

                    String openBank = obj.optString("openBank");
                    String accountName = obj.optString("accountName");
                    String bankNo = obj.optString("bankNo");
                    String financialPhone = obj.optString("financialPhone");

                    tvAccountName.setText(com.mytools.tool.utils.StringUtils.isBlank(accountName) ? "" : accountName);
                    tvBankName.setText(com.mytools.tool.utils.StringUtils.isBlank(accountName) ? "" : openBank);
                    etUserPhone.setText(com.mytools.tool.utils.StringUtils.isBlank(financialPhone) ? "" : financialPhone);
                    tvBankCard.setText(com.mytools.tool.utils.StringUtils.isBlank(bankNo) ? "" : StringUtils.formatCardNo(bankNo));
                } catch (Exception e) {
                    e.getMessage();

                }
            }

            @Override
            public void failed(Throwable error) {
                disShowProgress();
                showE404();
            }
        });
    }


    /**
     * 从服务器获取验证码
     */
    private void getValidCode() {
        MyCountDownTimer countDowntimer = new MyCountDownTimer(Config.VOLID_CODE_SECONDS, 1000, tvGetcode,
                getResources().getDrawable(R.drawable.reg_suc_bar1), getResources().getDrawable(R.drawable.reg_suc_bar2));
        countDowntimer.start();

        RequestParams params = new RequestParams();
        params.add("phone", getTvText(R.id.et_user_phone));
        params.add("groupId", MyApplication.getmInstance().getLoginMEntity().getGroupId() + "");
        params.add("type", "2");
        showProgress("正在发送");
        HttpUtil.get(Config.SBS_GET_CODE, params, new HttpUtil.RequestListener() {

            @Override
            public void success(String response) {
                disShowProgress();
                try {
                    JSONObject result = new JSONObject(response);
                    if (result.optInt("code") == -2) {
                        UtilPreference.clearNotKeyValues(mContext);
                        // 退出账号 返回到登录页面
                        MyActivityManager.getInstance().logout(mContext);
                        return;
                    }
                    if (!result.optBoolean("status")) {
                        ToastUtils.CustomShow(mContext, result.optString("message"));
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void failed(Throwable error) {
                disShowProgress();
                showE404();
            }

        });
    }


    @OnClick({R.id.tv_getcode, R.id.btn_commit, R.id.titleback_btn_back, R.id.all_tx})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_getcode:
                if (StringUtils.isBlank(getTvText(R.id.et_user_phone))) {
                    ToastUtils.showShort(this, "手机号不能为空");
                    return;
                }
                getValidCode();
                break;
            case R.id.btn_commit:
                doCommit();
                break;
            case R.id.titleback_btn_back:
                finish();
                break;
            case R.id.all_tx:
                etTxAmt.setText(tvBalance.getText().toString().trim());
                etTxAmt.setSelection(etTxAmt.getText().length());
                break;
        }
    }

    private void doCommit() {

        if (StringUtils.isBlank(etTxAmt.getMoneyText().trim())) {
            ToastUtils.showShort(mContext, "请输入提现金额");
            return;
        }

        if (StringUtils.isBlank(etUserPhone.getText().toString().trim())) {
            ToastUtils.showShort(mContext, "请输入手机号");
            return;
        }

        if (StringUtils.isBlank(getTvText(R.id.et_tx_code))) {
            ToastUtils.showShort(mContext, "请输入验证码");
            return;
        }


        showProgress("加载中...");

        String url = Config.SBS_TX;
        RequestParams params = new RequestParams();
        params.put("shopId", MyApplication.getmInstance().getLoginMEntity().getShopid() + "");
        params.put("amount", StringUtils.changeY2F(etTxAmt.getMoneyText().trim()));
        params.put("phone", getTvText(R.id.et_user_phone));
        params.put("balance", balanceAmt);
        params.put("code", getTvText(R.id.et_tx_code));
        params.put("type", "2");

        HttpUtil.get(url, params, new HttpUtil.RequestListener() {

            @SuppressWarnings("unchecked")
            @Override
            public void success(String response) {
                disShowProgress();
                try {


                    JSONObject result = new JSONObject(response);
                    if (!result.optBoolean("status")) {
                        showErrorMsg(result.getString("message"));
                        return;
                    }

//                    showErrorMsg(result.getString("message"));
                    startActivity(new Intent(mContext, MyTxSuccessActivity.class));
                    finish();




                } catch (Exception e) {
                    e.getMessage();

                }
            }

            @Override
            public void failed(Throwable error) {
                disShowProgress();
                showE404();
            }
        });
    }


}
